﻿function PageNavgation(prePath)
{
	var handle,array,allCount=0,currentPage=1,allPage=1,pageSize=5,call,bindObject,attributes=new Object();
	var paddingLeft=5;
	var args=new Object();
	var isClicked=false;
	//variables
	var aFirst=document.createElement("div");
	//aFirst.type="button";
	aFirst.onclick=function(){handle.firstPage();};
	aFirst.innerHTML="首页";
	aFirst.className="pageNav_first";
	
	var aPre=document.createElement("div");
	//aPre.type="button";
	aPre.onclick=function(){handle.prePage();};
	aPre.innerHTML="上一页";
	aPre.className="pageNav_pre";
	
	var aNext=document.createElement("div");
	//aNext.type="button";
	aNext.onclick=function(){handle.nextPage();};
	aNext.innerHTML="下一页";
	aNext.className="pageNav_next";
	 
	var aLast=document.createElement("div");
	//aLast.type="button";
	aLast.onclick=function(){handle.lastPage();};
	aLast.innerHTML="尾页";
	aLast.className="pageNav_last";
	
	var jumpText=document.createElement("div");
	jumpText.className="pageNav_number";
	this.getJumpText=function(){return jumpText;}
	
	var jumpTextNumber=document.createElement("input");
	jumpTextNumber.type="text";
	jumpTextNumber.border="0px";
	jumpTextNumber.style.transparent="true";
	jumpTextNumber.className="page_nav_text";
	jumpTextNumber.style.mozTtransition="none";
	jumpTextNumber.style.background="transparent";
	jumpTextNumber.style.backgroundColor="transparent";
	jumpTextNumber.style.boxShadow="none";
	jumpTextNumber.style.border="0px";
	jumpTextNumber.padding="0px";
	jumpTextNumber.style.padding="0px";
	jumpTextNumber.margin="0px";
	jumpTextNumber.style.margin="0px";
	
	jumpText.appendChild(jumpTextNumber);
	this.getJumpTextNumber=function(){return jumpTextNumber;}

	jumpTextNumber.onkeydown=function(event)
	{
		var keyCode=document.all?window.event.keyCode:event.which;
		if(keyCode == 13)
		{
			handle.jumpPage();
		}
	}

	var jumpButton=document.createElement("div");
	//jumpButton.type="button";
	jumpButton.innerHTML="跳转";
	jumpButton.className="pageNav_jump";

	jumpButton.onclick=function(){handle.jumpPage();};
	this.getJumpButton=function(){return jumpButton;}
	
	var infoSpan=document.createElement("div");
	infoSpan.className="nocurrentpagenumber";
	
	this.setCall=function(c){call=c;}
	this.setHandle=function(h){handle=h;}
	this.setArgs=function(as){args=as;}
	this.getArgs=function(){return args;}
	this.setPageSize=function(size)
	{
		pageSize=size;
		if(allCount==1){return;}
		if((allCount%pageSize) == 0)
		{
			allPage=allCount/pageSize;
		}else
		{
			allPage=Math.floor((allCount/pageSize)) +1;
		}
		allPage=Math.floor(allPage);
		if(0 == allPage){allPage=1;}
		infoSpan.innerHTML=currentPage+"/"+allPage;
	}
	//
	this.setArray=function(arr)
	{
		array=arr;
		allCount=array.length;
		if((allCount%pageSize) == 0)
		{
			allPage=allCount/pageSize;
		}else
		{
			allPage=(allCount/pageSize) +1;
		}
		allPage=Math.floor(allPage);
		if(0 == allCount){allPage=1;}
	}
	this.getArray=function(){return array;}
	//
	this.setAllCount=function(allc)
	{
		allCount=allc;
		if((allCount%pageSize) == 0)
		{
			allPage=allCount/pageSize;
		}else
		{
			allPage=Math.floor((allCount/pageSize)) +1;
		}
		allPage=Math.floor(allPage);
		if(0 == allPage){allPage=1;}
		infoSpan.innerHTML=currentPage+"/"+allPage;
	}
	this.addCount=function(c)
	{
		allCount=allCount+c;
		if((allCount%pageSize) == 0)
		{
			allPage=allCount/pageSize;
		}else
		{
			allPage=Math.floor((allCount/pageSize)) +1;
		}
		allPage=Math.floor(allPage);
		if(0 == allPage){allPage=1;}
		infoSpan.innerHTML=currentPage+"/"+allPage;
	}
	//init html to paint
	var container=document.createElement("div");
	container.className="pagenavgationcontainer";
	container.align="center";
	var table=document.createElement("table");
	table.className="pagenavgationcontainertable";
	var tbody=document.createElement("tbody");
	var tr=document.createElement("tr");
	var td1=document.createElement("td");
	var td2=document.createElement("td");
	var td3=document.createElement("td");
	var td4=document.createElement("td");
	var td5=document.createElement("td");
	var td6=document.createElement("td");
	var td7=document.createElement("td");
	td1.style.paddingLeft=paddingLeft+"px";
	td2.style.paddingLeft=paddingLeft+"px";
	td3.style.paddingLeft=paddingLeft+"px";
	td4.style.paddingLeft=paddingLeft+"px";
	td5.style.paddingLeft=paddingLeft+"px";
	td6.style.paddingLeft=paddingLeft+"px";
	td7.style.paddingLeft=paddingLeft+"px";
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	tr.appendChild(td6);
	tr.appendChild(td7);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	container.appendChild(table);
	td1.appendChild(aFirst);
	td2.appendChild(aPre);
	td3.appendChild(infoSpan);
	td4.appendChild(aNext);
	td5.appendChild(aLast);
	td6.appendChild(jumpText);
	td7.appendChild(jumpButton);

	this.initHtml=function()
	{
		//alert("initHtml..."+currentPage);
		
		infoSpan.innerHTML=currentPage+"/"+allPage;
		jumpTextNumber.value=currentPage;
		//handle call
		if(undefined == array)
		{
			if(isClicked)
			{
				call(currentPage,args);
			}
		}else
		{
			var tempArray=new Array();
			var start=pageSize*(currentPage-1);
			var end=pageSize*currentPage;
			if(end > array.length)
			{
				end=array.length;
			}
			for(var i=start;i<end;i++)
			{
				tempArray.push(array[i]);
			}
			call(tempArray,args);
		}
	}
	this.bind=function(id)
	{
		if(typeof(id) == "object")
		{
			bindObject=id;
			bindObject.innerHTML="";
			bindObject.appendChild(container);
		}else if(document.getElementById(id))
		{
			bindObject=document.getElementById(id);
			bindObject.innerHTML="";
			bindObject.appendChild(container);
		}else{ 
			return;
		}
		if(undefined != handle){handle.initHtml();};
	}
	this.setCurrentPage=function(c){
		currentPage=c;
		handle.initHtml();
	}
	//
	this.firstPage=function()
	{
		if(currentPage == 1){return;}
		isClicked=true;
		currentPage=1;
		handle.initHtml();
	}
	this.prePage=function()
	{
		if(currentPage == 1){return;}
		isClicked=true;
		currentPage--;
		handle.initHtml();
	}
	this.nextPage=function()
	{
		if(currentPage == allPage){return;}
		isClicked=true;
		currentPage++;
		handle.initHtml();
	}
	this.lastPage=function()
	{
		if(currentPage == allPage){return;}
		isClicked=true;
		currentPage=allPage;
		handle.initHtml();
	}
	this.jumpPage=function()
	{
		var jumpValue=jumpTextNumber.value;
		try
		{
			jumpNumber=parseInt(jumpValue);
			if((jumpNumber>0)&&(jumpNumber <= allPage)&&(jumpNumber != currentPage))
			{
				currentPage=jumpNumber;
				isClicked=true;
				handle.initHtml();
			}
		}catch(e){}
	}
	this.getAttributes=function(){return attributes;}
}
